import {submit} from "../detect";
import {request} from "../../../api/request";

Page({
    data: {
        tagOptions: [
            '健康',
            '国际',
            '生活',
            '政治与政策',
            '科技资安',
            '科学研究',
            '环境能源',
            '科技',
            '医药健康',
            '灾难事故',
            '文体娱乐',
            '社会生活',
            '军事',
            '政治',
            '教育考试',
            '财经商业',
            '其他'
        ],
        title: '',
        use_llm: false,
        content: '',
        tag: '健康',
        url: "",
        tagIndex: 0,
        contentLength: 0,
        tooltipVisible: false
    },

    onLoad() {
        const newsInfo = wx.getStorageSync('news_info')
        const newsUrl = wx.getStorageSync('news_url')
        if (newsInfo && newsUrl) {
            this.setData({
                content: newsInfo.content,
                title: newsInfo.title,
                tagIndex: newsInfo.tagIndex,
                contentLength: newsInfo.contentLength,
                url: newsUrl
            })
        }
    },
    onUnload() {
        // 清理页面资源
        wx.removeStorageSync('news_info')
        wx.removeStorageSync('news_url')
    },

    onTagChange(e) {
        const index = e.detail.value;
        this.setData({
            tagIndex: index,
            tag: this.data.tagOptions[index] // 将选中的标签赋值给 tag
        });
    },


    getNewsInfo(e) {
        const url = e.detail.value
        this.setData({
            url
        })
        const {tagIndex, contentLength} = this.data
        request({
            url: '/news/get_news_info',
            method: 'POST', // 或者 'POST'、'PUT' 等，根据接口要求确定
            data: url,
            // 要传递的参数对象，如果是 GET 请求可以放在 url 中作为查询参数
            header: {
                // 设置请求头，例如可以设置 Content-Type、Authorization 等
                'Content-Type': 'application/json'
            },
            fail: (err) => {
                // 请求失败后的处理逻辑
                console.error(err);
            }
        }).then(response => {
            // 请求成功后的处理逻辑
            console.log(response)
            const res = response.data
            if (res.code === 0) {
                this.setData({
                    contentLength: res.data.content.length,
                    title: res.data.title,
                    content: res.data.content,
                })
                const newsInfo = {...res.data, tagIndex, contentLength}
                wx.setStorageSync('news_info', newsInfo)
                wx.setStorageSync('news_url', url)
            }
            if (res.code === 40010) {
                wx.showToast({
                    title: '该平台还未适配，敬请期待',
                    icon: 'none'
                })
            }
        });

    },
    // 内容输入监听
    handleContentInput(e) {
        this.setData({
            content: e.detail.value,
            contentLength: e.detail.value.length
        })
    },

    handleUseLLMChange(e) {
        this.setData(
            {
                use_llm: !this.data.use_llm
            }
        )
    },
    // 表单提交
    submitForm(e) {
        const {title, content} = e.detail.value
        const tagIndex = this.data.tagIndex

        if (!title || !content) {
            wx.showToast({title: '请填写完整信息', icon: 'none'})
            return
        }

        if (content.length < 10) {
            wx.showToast({title: '内容不少于10字', icon: 'none'})
            return
        }

        let news = {
            content,
            title,
            tag: tagIndex,
            url: this.data.url || ""
        }
        // wx.setStorageSync("news_info", news)
        // wx.setStorageSync("news_url", news.url)
        submit(news, "/pages/detect/fast/index")
        // 表单提交
    }
})
